package com.libre.qr;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.krysalis.barcode4j.ChecksumMode;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.upcean.UPCABean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

public final class BarcodeUtils {
    public static void createUPCABarcodeImage(String text, String content, String savePath) throws IOException {
        // 1、生成UPC条形码码
        UPCABean bean = new UPCABean();
        // 1.1 设置条形码参数
        final int dpi = 250; // 分辨率
        bean.setModuleWidth(0.20); // makes the narrow bar
        // width exactly one pixel
        bean.doQuietZone(true);
        bean.setBarHeight(7);
        bean.setChecksumMode(ChecksumMode.CP_AUTO);
        bean.setFontSize(2.4);
        bean.setQuietZone(3); // 留白
        bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);

        // Set up the canvas provider for monochrome PNG output
        BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

        // 1.2 生成条形码
        bean.generateBarcode(canvas, content);
        // 2、将生成的条形码画出来
        BufferedImage barCodeImage = canvas.getBufferedImage();
        // 2.1 设置宽、高、图片类型
        BufferedImage newImage = new BufferedImage(394, 150, BufferedImage.TYPE_INT_RGB);
        // 绘图
        Graphics g2d = newImage.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());
        g2d.drawImage(barCodeImage,
                (newImage.getWidth() - barCodeImage.getWidth()) / 2,
                (newImage.getHeight() - barCodeImage.getHeight()) / 2 + 8,
                barCodeImage.getWidth(),
                barCodeImage.getHeight(),
                null);
        // 创建字体
        Font font = new Font("Helvetica", Font.PLAIN, 20);
        g2d.setFont(font);
        FontMetrics fm = g2d.getFontMetrics();
        g2d.setColor(Color.BLACK);
        // 绘制文本
        g2d.drawString(text, (newImage.getWidth() - fm.stringWidth(text)) / 2, 25);
//        ImageIO.write(newImage, "png", out);
        ImageIO.write(newImage, "png", new File(savePath));
        canvas.finish();
    }


    private static String path = System.getProperty("user.dir") + "\\src\\main\\resources\\qr\\";
    public static void main(String[] args) throws IOException {
        String msg = "这是信息";
        createUPCABarcodeImage( msg, "12345678901",path+"11.png");
    }

    // switch (barcodeType) {
    //    case CODE128               : this.barcode = new Code128Bean(); break;
    //    case CODABAR               : this.barcode = new CodabarBean(); break;
    //    case CODE39                : this.barcode = new Code39Bean(); break;
    //    case EAN128                : this.barcode = new EAN128Bean(); break;
    //    case DATAMATRIX            : this.barcode = new DataMatrixBean(); break;
    //    case INT2OF5               : this.barcode = new Interleaved2Of5Bean(); break;
    //    case PDF417                : this.barcode = new PDF417Bean(); break;
    //    case POSTNET               : this.barcode = new POSTNETBean(); break;
    //    case EAN13                 : this.barcode = new EAN13Bean(); break;
    //    case EAN8                  : this.barcode = new EAN8Bean(); break;
    //    case UPCA                  : this.barcode = new UPCABean(); break;
    //    case UPCE                  : this.barcode = new UPCEBean(); break;
    //    case ROYAL_MAIL_CBC        : this.barcode = new RoyalMailCBCBean(); break;
    //    case USPS_INTELLIGENT_MAIL : this.barcode = new USPSIntelligentMailBean(); break;

}

